<?php

namespace Frieza\Modules\Position\Actions;

use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Facades\Admin;

use Doraemon\model\AdmSaleBind\AdmSaleBind;
use Doraemon\service\admin\SaleBind;

class SaleBindAction extends RowAction
{
    public $name = '销售认领客户';

    public function handle($uid)
    {
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;
        $res = SaleBind::bind($uid, $admin_id, $admin_name);
        if ($res == SaleBind::STATUS_SUCCESS) {
            return $this->response()->success('认领成功')->refresh();
        } else if ($res == SaleBind::STATUS_REPEAT) {
            return $this->response()->success('已经认领,不需要重复操作');
        } else if ($res == SaleBind::STATUS_FAIL_BEBIND) {
            return $this->response()->error('认领失败，已被别人认领');
        } else if ($res == SaleBind::STATUS_FAIL_LIMIT) {
            return $this->response()->error('认领失败，超过认领上限:' . SaleBind::BIND_LIMIT);
        } else {
            return $this->response()->error('认领失败');
        }
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }
}
